#
# example006.py
#
# This example shows basig logarithmic quantification
#
# Copyright (C) 2012 Robert Buj Gelonch
# Copyright (C) 2012 David Megias Jimenez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
__author__ = "Robert Buj Gelonch, and David Megias Jimenez"
__copyright__ = "Copyright 2012, Robert Buj Gelonch and David Megias Jimenez"
__credits__ = ["Robert Buj Gelonch", "David Megias Jimenez"]
__license__ = "GPL"
__version__ = "3"
__maintainer__ = "Robert Buj"
__email__ = "rbuj@uoc.edu"
__status__ = "Development"
__docformat__ = 'plaintext'

from numpy import arange
from numpy import fix
from numpy import log10
from numpy import sort
from pylab import figure
from pylab import grid
from pylab import loglog
from pylab import plot
from pylab import show
from pylab import subplot
from pylab import subplot2grid
from pylab import title

print "example006.py"
print

#--------------------------------------------------------------
# Logarithmic quantification
#--------------------------------------------------------------
max_value = 10.0 ** 5
data = arange(1.0, max_value)

aux_10 = 10 ** fix(log10(data))
steps_10 = sort(list(set(aux_10)), kind='quicksort')
print 'base: 10'
print "Number of levels:", len(set(aux_10))
print 'Steps:', steps_10
print ''

# modify the value of num_steps to increase number od levels
# > 10 to increase precision
num_levels = 20.0
steps = max_value ** (1.0 / num_levels)
aux_steps = steps ** fix(log10(data) / log10(steps))
val_steps = sort(list(set(aux_steps)), kind='quicksort')
print 'base:', steps
print "Number of levels:", len(set(aux_steps))
print 'Steps:', val_steps

#--------------------------------------------------------------
# Plotting
#--------------------------------------------------------------
fig = figure(num=1, \
             figsize=(14, 9), \
             dpi=80, \
             facecolor='w', \
             edgecolor='k')
subplot2grid((2, 3), (0, 0), colspan=2)
title('Logarithmic plot')
loglog(data, 'b')
loglog(aux_10, 'r')
loglog(aux_steps, 'g')
grid(True, which='both')

subplot2grid((2, 3), (1, 0), colspan=2)
title('Linear plot')
plot(data, 'b')
plot(aux_10, 'r')
plot(aux_steps, 'g')
grid(True)

subplot(233)
title('%d steps' % len(steps_10))
plot(steps_10, 'ro-')
grid(True, which='both')

subplot(236)
title('%d steps' % len(val_steps))
plot(val_steps, 'go-')
grid(True, which='both')

# Save plot
plt.savefig("../plots/example006.py.1.png", format="png")

# Show plot
show()

print "Done"